package com.hongdee.crm.cartag.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.cartag.entity.TCrmSurveryQuestion;
import com.hongdee.crm.cartag.service.ITCrmCartagService;
import com.hongdee.crm.wo.entity.TCrmWoClaim;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by 侯念晨 on 2017-05-12.
 */
@RestController
@RequestMapping("/crm/cartag")
public class ITCrmCartagController {
    @Autowired
    private ITCrmCartagService itCrmCartagService;

    /*
  * 查询详情接口
  * */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        EntityUtils.valid(id);
        ResultEntity res = new ResultEntity();
        Map map = itCrmCartagService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    /*
 * 分页
 * */
    @RequestMapping("/page")
    @ResponseBody
    public Map newPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "start", defaultValue = "0") String start,
                       @RequestParam(name = "limit", defaultValue = "50") String limit,
                       @RequestParam(name = "value", defaultValue = "") String value
    ) throws Exception {

        Map map = this.itCrmCartagService.newPage(value,start,limit,uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 停用工单类别对应接口
     */
    @RequestMapping("/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        EntityUtils.valid(id);
        ResultEntity res = new ResultEntity();
        itCrmCartagService.update(id, "1");
        res.setSuccess(true);
        res.setMsg("停用成功！");
        return res;
    }
    /**
     * 启用维系类别接口
     */
    @RequestMapping("/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        EntityUtils.valid(id);
        ResultEntity res = new ResultEntity();
        itCrmCartagService.update(id, "0");
        res.setSuccess(true);
        res.setMsg("启动成功！");
        return res;
    }
    /**
     * 保存接口
     * */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TCrmSurveryQuestion tCrmSurveryQuestion) throws Exception {
        EntityUtils.valid(tCrmSurveryQuestion);
        TCrmSurveryQuestion group = itCrmCartagService.saveOrUpdate(tCrmSurveryQuestion,uid);
        ResultEntity res = new ResultEntity();
        res.setData(group);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }
    /**
     * 获取树接口
     */
    @RequestMapping("/leftTree")
    @ResponseBody
    public ResultEntity leftTree(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        List list = this.itCrmCartagService.tree();
        res.setDataList(list);
        res.setSuccess(true);
        res.setMsg("查询资源树");
        return res;
    }
    /**
     * 查询问题项
     */
//    @RequestMapping("/itemPage")
//    @ResponseBody
//    public ResultEntity itemPage(@RequestParam(name = "uid", defaultValue = "") String uid,
//                             @RequestParam(name = "token", defaultValue = "") String token,
//                             @RequestParam(name = "groupCode", defaultValue = "") String groupCode) throws Exception {
//        EntityUtils.valid(groupCode);
//        ResultEntity res = new ResultEntity();
//        Map map = itCrmCartagService.itemPage(groupCode);
//        res.setData(map);
//        res.setSuccess(true);
//        res.setMsg("查询成功");
//        return res;
//    }

    /**
     * 保存选中的树数据
     * @param uid
     * @param token
     * @param questionId
     * @param itemIds
     * @return
     * @throws Exception
     */

    @RequestMapping("/saveItemTree")
    @ResponseBody
    public ResultEntity saveItemTree(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestParam(name = "questionId", defaultValue = "") String questionId,
                                @RequestBody String[] itemIds) throws Exception {
        if (StringUtils.isEmpty(questionId)) {
            throw new Exception("请选择角色");
        }
        List list = this.itCrmCartagService.saveItemTree(questionId, itemIds);
        ResultEntity res = new ResultEntity();
        res.setDataList(list);
        res.setMsg("保存成功！");
        res.setSuccess(true);
        return res;

    }

    /**
     *问题项查询
     */
    @RequestMapping("/groupPage")
    @ResponseBody
    public Map groupPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                         @RequestParam(name = "token", defaultValue = "") String token,
                         @RequestParam(name = "start", defaultValue = "0") String start,
                         @RequestParam(name = "limit", defaultValue = "50") String limit,
                         @RequestParam(name = "questionId", defaultValue = "") String questionId,
                         @RequestParam(name = "groupCode", defaultValue = "") String groupCode
    ) throws Exception {
        Map map = this.itCrmCartagService.groupPage(questionId,groupCode,start,limit);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }
    /**
     *评分项查询
     */
    @RequestMapping("/itempage")
    @ResponseBody
    public Map itempage(@RequestParam(name = "uid", defaultValue = "") String uid,
                        @RequestParam(name = "token", defaultValue = "") String token,
                        @RequestParam(name = "start", defaultValue = "0") String start,
                        @RequestParam(name = "limit", defaultValue = "50") String limit,
                        @RequestParam(name = "itemId", defaultValue = "") String itemId
    ) throws Exception {
        Map map = this.itCrmCartagService.itempage(itemId, start, limit);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

}
